"""
Case Type   : 兼容M*数据库time类型
Case Name   : mot表使用time类型
Create At   : 2022.11.25
Owner       : z@wan005
Description :
    1、设置enable_incremental_checkpoint=off
    2、创建mot表
    3、插入数据
    4、查询
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
    modified by lvlintao at 2025/3/24，优化用例，适配MOT开关
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest45(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0045"
        self.tb_name = "tb_time_0045"
        self.default_vales = self.common.show_param(
            'enable_incremental_checkpoint')
        self.log.info(self.default_vales)
        self.mot_engine = "enable_mot_server"
        text = '--------开启mot特性  ' \
               'expect:修改成功，重启数据库成功--------'
        self.log.info(text)
        self.engine_res = self.common.show_param(f'{self.mot_engine}')
        self.sh_pri.execute_gsguc('set',
                                  self.constant.GSGUC_SUCCESS_MSG,
                                  f'{self.mot_engine} = on')
        self.sh_pri.restart_db_cluster()
        status = self.sh_pri.get_db_cluster_status()
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

    def test_utc_date(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '-----step1:设置enable_incremental_checkpoint=off;' \
               'expect:成功-----'
        self.log.info(text)
        if 'off' not in self.default_vales:
            set_off = self.sh_pri.execute_gsguc(
                "set", self.constant.GSGUC_SUCCESS_MSG,
                f'enable_incremental_checkpoint=off')
            self.log.info(set_off)
            status = self.sh_pri.restart_db_cluster()
            self.log.info(status)
            res = self.common.show_param('enable_incremental_checkpoint')
            self.log.info(res)
            self.assertIn("off", res, "执行失败：" + text)

        text = '--step2:创建mot表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create foreign table {self.tb_name}(c1 time);"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('CREATE FOREIGN TABLE', sql_res, '执行失败' + text)

        text = '--step3:插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} values('-838:59:59');" \
                  f"insert {self.tb_name} values('838:59:59');" \
                  f"insert {self.tb_name} values(time '838:59:59');" \
                  f"insert {self.tb_name} values('837:59:59.9999999');" \
                  f"insert {self.tb_name} values('00:00:00');" \
                  f"insert {self.tb_name} values('0');" \
                  f"insert {self.tb_name} values('24:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(7, sql_res.count('INSERT 0 1'), '执行失败' + text)

        text = '--step4:查询;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_pri.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}')
        self.log.info(sql_res)
        res_list = ['-838:59:59', '838:59:59', '838:59:59', '838:00:00',
                    '00:00:00', '00:00:00', '24:59:59']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};checkpoint;"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_pri.execut_db_sql(sql_cmd,
                                              dbname='postgres')
        self.log.info(drop_res1)
        gs_guc = self.sh_pri.execute_gsguc("set",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f'enable_incremental_checkpoint'
                                           f'={self.default_vales}')
        self.log.info(gs_guc)
        text = '--------还原mot特性  ' \
               'expect:修改成功，重启数据库成功--------'
        self.log.info(text)
        self.sh_pri.execute_gsguc('set',
                                  self.constant.GSGUC_SUCCESS_MSG,
                                  f'{self.mot_engine} = {self.engine_res}')
        status = self.sh_pri.restart_db_cluster()
        self.log.info(status)
        res = self.common.show_param('enable_incremental_checkpoint')
        self.log.info(res)
        self.assertIn("on", res, "执行失败：" + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
